package com.gulimall.ware.config;

import com.gulimall.common.constrant.WareConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author kkk
 * @date 2022/8/6
 */
@Configuration
public class RabbitMqConfig {
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public TopicExchange stockEventExchange() {
        return new TopicExchange(WareConstant.WARE_STOCK_EVENT_EXCHANGE, true, false);
    }

    @Bean
    public Queue stockReleaseQueue() {
        return new Queue(WareConstant.WARE_STOCK_RELEASE_QUEUE, true, false, false);
    }

    @Bean
    public Queue stockDelayQueue() {
        Map<String, Object> param = new HashMap<>();
        param.put("x-dead-letter-exchange", WareConstant.WARE_STOCK_EVENT_EXCHANGE);
        param.put("x-message-ttl", WareConstant.TTL_TIME);
        param.put("x-dead-letter-routing-key", WareConstant.WARE_STOCK_RELEASE_KEY);
        return new Queue(WareConstant.WARE_STOCK_DELAY_QUEUE, true, false, false, param);
    }

    @Bean
    public Binding stockDelayBinding() {
        return new Binding(WareConstant.WARE_STOCK_DELAY_QUEUE, Binding.DestinationType.QUEUE,
                WareConstant.WARE_STOCK_EVENT_EXCHANGE, WareConstant.WARE_STOCK_LOCKED_KEY, null);
    }

    @Bean
    public Binding stockReleaseBinding() {
        return new Binding(WareConstant.WARE_STOCK_RELEASE_QUEUE, Binding.DestinationType.QUEUE,
                WareConstant.WARE_STOCK_EVENT_EXCHANGE, WareConstant.WARE_STOCK_RELEASE_KEY, null);
    }

}
